package com.dlwx.parttimetalent.ui;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.TuiGuangBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.wechatpay.wxapi.WXShareUtiles;
import com.dlwx.parttimetalent.weibo.WeiboUitls;
import com.dlwx.parttimetalent.wxapi.QQUtiles;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class TuiGuangActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_qq)
    TextView tvQq;
    @BindView(R.id.tv_space)
    TextView tvSpace;
    @BindView(R.id.tv_friend)
    TextView tvFriend;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    @BindView(R.id.tv_weibo)
    TextView tvWeibo;
    @BindView(R.id.title_right_one_btn)
    ImageView titleRightOneBtn;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.title_right_two_btn)
    ImageView titleRightTwoBtn;
    @BindView(R.id.tv_tuiguang)
    TextView tvTuiguang;
    @BindView(R.id.tv_fctext)
    TextView tvFctext;
    private QQUtiles qqUtiles;
    private WXShareUtiles wxShareUtiles;
    private Bitmap bitmap;
    private RxPermissions rxPermissions;
    private String shareUrl,qrCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tui_guang);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void initView() {
        rxPermissions = new RxPermissions(this);
        qqUtiles = new QQUtiles();
        wxShareUtiles = new WXShareUtiles(this);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.logo_share);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.myteam);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        titleRightOneTv.setText("查看");
        titleRightOneTv.setTextSize(13);
        titleRightOneTv.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
        titleRightOneTv.setCompoundDrawables(null, drawable, null, null);
        tvMiddleTitle.setText("我的赚钱团队(0)");
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.getShareInfo, TuiGuangActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        TuiGuangBean parentBean = DealResult.getInstance().dealDatas(TuiGuangActivity.this, response, new TypeToken<TuiGuangBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        }
//                        ToastUtils.toastForShort(TuiGuangActivity.this,parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(TuiGuangActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private int peopleNum = 0;

    private void setDatas(TuiGuangBean.BodyBean body) {
        peopleNum = body.getPeoplenum();
        tvMiddleTitle.setText("我的赚钱团队" + "(" + peopleNum + ")");
        qrCode = body.getQrcode();
        shareUrl = body.getTeamurl();
        String content = body.getTextcontent().replaceAll("#", "\n");
        tvTuiguang.setText(content);
        tvFctext.setText(body.getFctext());
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_qq, R.id.tv_space, R.id.tv_friend, R.id.tv_wechat, R.id.tv_weibo, R.id.title_right_one_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_qq:
                qqUtiles.shareQQ(this, qrCode);
                break;
            case R.id.tv_space:
                rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            qqUtiles.shareImageQQZone(TuiGuangActivity.this, qrCode);
                        }
                    }
                });

                break;
            case R.id.tv_friend:
                wxShareUtiles.share2weixin(2, bitmap, qrCode);
                break;
            case R.id.tv_wechat:
                wxShareUtiles.share2weixin(1, bitmap, qrCode);
                break;
            case R.id.tv_weibo:
                WeiboUitls weiboUitls = new WeiboUitls(this);
                weiboUitls.share(qrCode);
                break;
            case R.id.title_right_one_tv:
                Intent i = new Intent(this, TeamActivity.class);
                i.putExtra("num", peopleNum);
                i.putExtra("qrCode", qrCode);
                i.putExtra("shareUrl", shareUrl);
                startActivity(i);
                break;
        }
    }
}
